/**
* @Title: Demo2.java
* @Package me.shibor.study.javaSE.database
* @Description: TODO
* @author shibor
* @date 2017年2月9日 下午11:41:44
* @version V1.0
*/
package me.shibor.study.javaSE.database;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

/**
 * @ClassName: Demo2
 * @Description: Java应用程序访问数据库。应用程序打开考生信息表ksInfo，从中取出考生的各项信息。
 * @author shibor
 * @date 2017年2月9日 下午11:41:44
 * 
 */
public class Demo2 extends JFrame implements ActionListener {
	
	String title[] = { "ID", "姓名", "Email", "地址", "简历" };
	JTextField txtNo = new JTextField(8);
	JTextField txtName = new JTextField(10);
	JTextField txtScore = new JTextField(3);
	JTextField txtAddr = new JTextField(30);
	JTextArea txtresume = new JTextArea();
	JButton prev = new JButton("前一个");
	JButton next = new JButton("后一个");
	JButton first = new JButton("第一个");
	JButton last = new JButton("最后一个");
	Statement sql; // SQL语句对象
	ResultSet rs; // 存放查询结果对象

	Demo2(Connection connect){
        super("考生信息查看窗口");
        setSize(450, 350);
        try{
            sql = connect.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
            rs = sql.executeQuery("SELECT * FROM student");
            Container con = getContentPane();
            con.setLayout(new BorderLayout(0, 6));
            JPanel p[] = new JPanel[4];
            for (int i = 0; i < 4; i++){
                p[i] = new JPanel(new FlowLayout(FlowLayout.LEFT, 8, 0));
                p[i].add(new JLabel(title[i]));
            }
            p[0].add(txtNo);
            p[1].add(txtName);
            p[2].add(txtScore);
            p[3].add(txtAddr);
            JPanel p1 = new JPanel(new GridLayout(4, 1, 0, 8));
            JScrollPane jsp = new JScrollPane(txtresume,
                JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
                JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
                jsp.setPreferredSize(new Dimension(300, 60));
            for (int i = 0; i < 4; i++){
                p1.add(p[i]);
            }
            JPanel p2 = new JPanel(new FlowLayout(FlowLayout.LEFT, 10, 0));
            p2.add(new JLabel(title[4]));
            p2.add(jsp);
            JPanel p3 = new JPanel();
            p3.add(prev);
            p3.add(next);
            p3.add(first);
            p3.add(last);
            prev.addActionListener(this);
            next.addActionListener(this);
            first.addActionListener(this);
            last.addActionListener(this);
            rs.first();
            readRecord();
        }
        catch (Exception e){
            e.printStackTrace();
        }
        setVisible(true);
    }

	public void modifyRecord(Connection connect){
        String stuNo = (String)JOptionPane.showInputDialog(null,
            "请输入考生考号", "输入考号对话框", JOptionPane.PLAIN_MESSAGE, null,
            null, "");
        try {
            sql = connect.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
            rs = sql.executeQuery("SELECT * FROM student");
            Container con = getContentPane();
            con.setLayout(new BorderLayout(0, 6));
            JPanel p[] = new JPanel[4];
            for (int i = 0; i < p.length; i++){
                p[i] = new JPanel(new FlowLayout(FlowLayout.LEFT, 8, 0));
                p[i].add(new JLabel(title[i]));
            }
            p[0].add(txtNo);
            p[1].add(txtName);
            p[2].add(txtScore);
            p[3].add(txtAddr);
            JPanel p1 = new JPanel(new GridLayout(4, 1, 0, 8));
            JScrollPane jsp = new JScrollPane(txtresume,
                JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
                JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
            jsp.setPreferredSize (new Dimension(300, 60));
            for (int i = 0; i < 4; i++){
                p1.add(p[i]);
            }
            JPanel p2 = new JPanel(new FlowLayout(FlowLayout.LEFT, 10, 0));
            p2.add(new JLabel(title[4]));
            p2.add(jsp);
            JPanel p3 = new JPanel();
            p3.add(prev);
            p3.add(next);
            p3.add(first);
            p3.add(last);
            prev.addActionListener(this);
            next.addActionListener(this);
            first.addActionListener(this);
            last.addActionListener(this);
            rs.first();
            readRecord();
        }
        catch (Exception e){
            e.printStackTrace();
        }
        setVisible(true);
    }

	boolean readRecord() {
		try {
			txtNo.setText(String.valueOf(rs.getInt("id")));
			txtName.setText(rs.getString("name"));
			txtScore.setText(String.valueOf(rs.getInt("score")));
			txtAddr.setText(rs.getString("address"));
			txtresume.setText(rs.getString("resume"));
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}

	public void actionPerformed(ActionEvent e) {
		try {
			if (e.getSource() == prev)
				rs.previous();
			else if (e.getSource() == next)
				rs.next();
			else if (e.getSource() == first)
				rs.first();
			else if (e.getSource() == last)
				rs.last();
			readRecord();
		} catch (Exception e2) {
		}
	}

	public static void main(String args[]) {
		Connection connect = null;
		JFrame.setDefaultLookAndFeelDecorated(true);
		
		Font font = new Font("JFrame", Font.PLAIN, 14);
		if ((connect = MysqlConnecter.getConnection()) == null) {
			JOptionPane.showMessageDialog(null, "数据库连接失败!");
			System.exit(-1);
		}
		new Demo2(connect); // 创建对象
	}
}